package com.fooddelivery;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RestaurantBrowserFrame extends JFrame {
    private JTextArea restaurantInfoArea;

    public RestaurantBrowserFrame() {
        setTitle("Restaurant Browser");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JTextField searchField = new JTextField();
        JButton searchButton = new JButton("Search");
        searchButton.addActionListener(e -> searchRestaurants(searchField.getText()));

        JPanel searchPanel = new JPanel();
        searchPanel.setLayout(new BorderLayout());
        searchPanel.add(searchField, BorderLayout.CENTER);
        searchPanel.add(searchButton, BorderLayout.EAST);

        restaurantInfoArea = new JTextArea();
        restaurantInfoArea.setEditable(false);

        panel.add(searchPanel, BorderLayout.NORTH);
        panel.add(new JScrollPane(restaurantInfoArea), BorderLayout.CENTER);

        add(panel);
        loadRestaurants();
    }

    private void loadRestaurants() {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM restaurants";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();

            StringBuilder sb = new StringBuilder();
            while (rs.next()) {
                sb.append("Name: ").append(rs.getString("name")).append("\n");
                sb.append("Location: ").append(rs.getString("location")).append("\n");
                sb.append("Cuisine Type: ").append(rs.getString("cuisine_type")).append("\n\n");
            }
            restaurantInfoArea.setText(sb.toString());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void searchRestaurants(String keyword) {
        try (Connection conn = DatabaseConnection.getConnection()) {
            String sql = "SELECT * FROM restaurants WHERE name LIKE ? OR location LIKE ? OR cuisine_type LIKE ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, "%" + keyword + "%");
            stmt.setString(2, "%" + keyword + "%");
            stmt.setString(3, "%" + keyword + "%");
            ResultSet rs = stmt.executeQuery();

            StringBuilder sb = new StringBuilder();
            while (rs.next()) {
                sb.append("Name: ").append(rs.getString("name")).append("\n");
                sb.append("Location: ").append(rs.getString("location")).append("\n");
                sb.append("Cuisine Type: ").append(rs.getString("cuisine_type")).append("\n\n");
            }
            restaurantInfoArea.setText(sb.toString());
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
